﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FirmaTransportowa
{
    public partial class DispatcherTransportsList : Form
    {
        SqlConnection dbConnection;

        public DispatcherTransportsList()
        {
            SqlConnector connection = new SqlConnector();
            dbConnection = connection.getInstance();
            InitializeComponent();
            displayTransports();
        }

        private void displayTransports()
        {
            SqlCommand command = new SqlCommand("SELECT t.transport_ID, t.status, t.transport_date, " +
                                    "t.transport_start_time, t.transport_end_time, t.start_city, " + 
                                    "t.destination_city, t.distance, v.brand, v.model, v.capacity, w.name, w.surname, d.name, d.surname " +
                                    "FROM (((Transport t JOIN Vehicles v ON t.vehicle_ID = v.vehicle_ID) JOIN Workers w ON w.worker_ID = t.worker_ID) " +
                                    "JOIN Workers d ON d.worker_ID = t.driver_ID)", dbConnection);
            SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
            DataTable dataTable = new DataTable();
            dataAdapter.Fill(dataTable);

            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = dataTable;
            transportsGridView.DataSource = bindingSource;
            transportsGridView.Columns[0].HeaderText = "ID";
            transportsGridView.Columns[1].HeaderText = "Status";
            transportsGridView.Columns[2].HeaderText = "Data transportu";
            transportsGridView.Columns[3].HeaderText = "Czas rozpoczęcia";
            transportsGridView.Columns[4].HeaderText = "Czas zakończenia";
            transportsGridView.Columns[5].HeaderText = "Miejscowość startowa";
            transportsGridView.Columns[6].HeaderText = "Miejscowość docelowa";
            transportsGridView.Columns[7].HeaderText = "Odległość[km]";
            transportsGridView.Columns[8].HeaderText = "Marka pojazdu";
            transportsGridView.Columns[9].HeaderText = "Model pojazdu";
            transportsGridView.Columns[10].HeaderText = "Ładowność";
            transportsGridView.Columns[11].HeaderText = "Imię dyspozytora";
            transportsGridView.Columns[12].HeaderText = "Nazwisko dyspozytora";
            transportsGridView.Columns[13].HeaderText = "Imię kierowcy";
            transportsGridView.Columns[14].HeaderText = "Nazwisko kierowcy";
            transportsGridView.Columns[0].Width = 100;
            transportsGridView.Columns[1].Width = 70;
            transportsGridView.Columns[2].Width = 65;
            transportsGridView.Columns[3].Width = 80;
            transportsGridView.Columns[4].Width = 80;
            transportsGridView.Columns[5].Width = 140;
            transportsGridView.Columns[6].Width = 140;
            transportsGridView.Columns[7].Width = 100;
            transportsGridView.Columns[8].Width = 120;
            transportsGridView.Columns[9].Width = 120;
            transportsGridView.Columns[10].Width = 80;
            transportsGridView.Columns[11].Width = 100;
            transportsGridView.Columns[12].Width = 100;
            transportsGridView.Columns[13].Width = 100;
            transportsGridView.Columns[14].Width = 100;
        }

        private void DispatcherTransportsList_Shown(object sender, EventArgs e)
        {
            try
            {
                dbConnection.Open();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        private void DispatcherTransportsList_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                dbConnection.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}
